import app from '@/App'

import personSelect from '@/components/asc/PersonSelect'

import {getCopySendItemsList,getProcInsTrace,copySendItemSubmitSecond} from '@/api/api.js'

export default {
    components:{
        app,
        personSelect
    },
    data() {
        return {
            iconDown:true,
            contentidshow:false,
            determinate:'',
            next_actor:'',
            deal_man:[],
            showCount:0,
            count: 0,
            pageSize:5,
            pageIndex:1,
            hasDealApprove:0, //已处理审批  处理5条自动去刷新页面
            //模拟数据
            data: [
                
            ],
            commitCount:0,//用于刷新滚动条
            busy: false,
            isKaiGuan:false,

        }
    },
    methods: {
        showDetails:function(item){
            //展示折叠框内容
            item.iconDown = !item.iconDown;
            item.contentidshow = !item.contentidshow;
            
        },
        loadMore:function(){
            
        },
        //查看详情
        gotoDetails:function(row){
            //console.log(row);

            this.isKaiGuan = false;
            //let flow = JSON.stringify(row);
            //query:{flow:encodeURIComponent(flow)}
            this.$router.push({path:"/need_deal_task_details/"+row.procInstID,query:{obj:row}});
        },
        //查看日志
        gotoLogs:function(row){
            this.isKaiGuan = false;
            this.$router.push({path:'/log',query:{obj:row}});
        },
        //查看上传文件
        gotoFiles:function(){
            this.isKaiGuan = false;
            this.$router.push("/download");
        },
        //快捷文字
        selectText:function(e,item){
            //文本内容
            let text = e.currentTarget.innerHTML;
            item.approveSuggest = text;
        },
        //处理流程
        approveFlow:function(data){
            let _this = this;
            this.$confirm("是否进行审批","提示",{
                confirmButtonText: _this.$t('te.confirm'),
                cancelButtonText: _this.$t('te.cancel'),
                cancelButtonClass: 'btn-second',
                confirmButtonClass: 'btn-default',
            }).then(function(){
                _this.data.splice(data,1);
                _this.data[0].iconDown = !_this.data[0].iconDown;
                _this.data[0].contentidshow = !_this.data[0].contentidshow;
                if(++_this.hasDealApprove == 5){
                    //进行翻页操作
                    _this.menu();
                    _this.hasDealApprove = 0;
                }
            })
            
        },
        menu:function(){
            if(this.isKaiGuan){
                //myScrollbar  在App.vue页面  滚动条  
                let let1 = this.$root.$children[0].$refs['myScrollbar'].wrap.scrollTop;
                let let2 = this.$root.$children[0].$refs['myScrollbar'].wrap.scrollHeight;
                let let3 = document.body.scrollHeight; 
                
                if((parseInt(let1 + let3) == parseInt(let2 + 17)) && this.showCount < this.count  ){
                    //模拟加载数据
                    this.showCount = this.showCount + 5;
                    this.isKaiGuan = false;
                    let loadingInstance = this.$loading({target:'#load',text:'加载中'});
                    let params = {
                        pageIndex: ++this.pageIndex,
                        pageSize: this.pageSize,
                        type:2,
                        t:Math.random()
                    }
                    this.showNeedReadList(params);
                    this.isKaiGuan = true;
                }
            }
        },
        showNeedReadList:function(params){
            //查询待阅列表页面
            let _this = this;
            getCopySendItemsList(params).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    let pageObj = data.Paging;
                    let dataList = data.ToDoTasks;
                    _this.showCount += pageObj.totalCount <= pageObj.pageSize ? pageObj.totalCount:pageObj.pageSize;
                    _this.count = pageObj.totalCount;
                    

                    for(let i = 0; i < dataList.length; i++){
                        dataList[i].lastApproveSuggest = '';
                        dataList[i].lastApproveMan = '';
                        dataList[i].lastApproveTime = '';
                        dataList[i].showProcLogs = [];
                        dataList[i].isHasReadTask = true; //保证是待阅任务 用于详情里面的字段与待办区分
                        _this.showProcLogAndLastaMan(dataList[i]);
                    }
                    _this.data.push(...dataList);      

                    //判断是否需要循环 判断当前页数是否大于3
                    //且数据满足大于15 10 条
                    if(_this.pageIndex < 3 && _this.showCount < _this.count ){
                        let params = {
                            pageIndex: ++_this.pageIndex ,
                            pageSize: _this.pageSize,
                            type:2,
                            t:Math.random()
                        }
                        _this.showNeedReadList(params);
                    }
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        showProcLogAndLastaMan:function(item){
            let _this = this;
            let params = {
                procInsId: item.procInstID,
                pageIndex: 1,
                pageSize: 1000,
                t:Math.random()
            };
            getProcInsTrace(params).then((result) => {
                if(result.status == "200" && result.data.code == "0"){
                    let data = result.data.data.data;
                    for(let i = 0; i < data.ProcessTraces.length; i++){
                        if(i == data.ProcessTraces.length-2){
                            //设置上一审批意见
                            let commont = data.ProcessTraces[i].processLogDTO.comment;
                            item.lastApproveSuggest = commont == ""?"暂无":commont;
                            item.lastApproveMan = data.ProcessTraces[i].processLogDTO.approverChsName;
                            item.lastApproveTime = data.ProcessTraces[i].processLogDTO.finishDate;
                        }
                        item.showProcLogs.push(data.ProcessTraces[i].processLogDTO);
                    }
                }else{
                    _this.$alert(result.data.msg,_this.$t('cm.tips'));
                }
            }).catch((err) => {
                _this.$alert(err,_this.$t('cm.tips'));
            });
        },
        transDate:function(row,col,value){    
            var time = new Date(value);
            var y = time.getFullYear();
            var m = time.getMonth()+1;
            var d = time.getDate();
            var h = time.getHours();
            var mm = time.getMinutes();
            var s = time.getSeconds();
            if(y == "0001"){
                return ;
            }
            return y+'-'+this.add0(m)+'-'+this.add0(d);//+' '+this.add0(h)+':'+this.add0(mm)+':'+this.add0(s)
        },
        add0:function(m){
            return m<10?'0'+m:m 
        },
        //已阅提交
        hasRead(item){
            let _this = this;
            this.$confirm("是否已阅","提示",{
                confirmButtonText: _this.$t('te.confirm'),
                cancelButtonText: _this.$t('te.cancel'),
                cancelButtonClass: 'btn-second',
                confirmButtonClass: 'btn-default',
            }).then(function(){
                let array = _this.data;
                copySendItemSubmitSecond({comment:"已阅",workId:item.sn}).then((result) => {
                    if(result.status == "200" && result.data.code == "0"){
                        _this.data.splice(array.findIndex(array => array.procInstID == item.procInstID),1);
                        _this.commitCount++;
                        if(_this.commitCount % 5 == 0){
                            //进行刷新页面操作  
                            let flushParam = {
                                pageIndex : _this.pageIndex,
                                pageSize : _this.pageSize,
                                type:2,
                                t:Math.random()
                            }
                            _this.showNeedReadList(flushParam);   
                        }
                    }else{
                        _this.$alert(result.data.msg,_this.$t('cm.tips'));
                    }
                }).catch((err) => {
                    _this.$alert(err,_this.$t('cm.tips'));
                });                   
            })
        }
    },
    mounted() {
        //  绝对不能根据滚动条的高度来进行调整
        //自动显示15条数据 每5条提交后自动刷新页面 还是判断没有滚动条自动刷新页面   给  count 和 showCount 赋值  模拟点击提交 
        //成功后删除该条数据，但要保证始终有滚动条，如果监听是否有滚动条来决定是否有数据 
        //当然 count和showCount要进行判断
        //首次加载3次，然后每次加载5个，后台也要处理 加载15个的原因在上面  这块不要了
        let params = {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            type:2,
            t:Math.random()
        }
        this.showNeedReadList(params);
        
        //判断滚动条位置，自动滑到最顶部
        // this.$root.$children[0].$refs['myScrollbar'].wrap.scrollTop = 0;
        this.isKaiGuan = true;
        window.addEventListener('scroll',this.menu,true);
    },
    destroyed() {
        //  删除监听事件
        window.removeEventListener('scroll',this.menu,true);
    },
}